﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using UtilityToolsCollect.ObjectsLibrary;

#pragma warning disable CA1416 // 验证平台兼容性
namespace ResolutionCalculator
{
    public partial class Form1 : Form
    {
        #region UI
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private IContainer? components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源，为 true；否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            ProportionComboBox = new ComboBox();
            panel1 = new Panel();
            AddProportion_Button = new Button();
            checkBL = new CheckBox();
            textHR = new TextBox();
            textWR = new TextBox();
            label3 = new Label();
            label1 = new Label();
            panel2 = new Panel();
            label7 = new Label();
            label4 = new Label();
            textH = new TextBox();
            textW = new TextBox();
            button_OpenFormZY = new Button();
            AddResolution_Button = new Button();
            checkSD = new CheckBox();
            label6 = new Label();
            ProportionLabel = new Label();
            panel3 = new Panel();
            ScreenCheck = new CheckBox();
            label8 = new Label();
            FilterResolutionFromProportions_ComboBox = new ComboBox();
            ElementView = new FlowLayoutPanel();
            panel6 = new Panel();
            panel1.SuspendLayout();
            panel2.SuspendLayout();
            panel3.SuspendLayout();
            panel6.SuspendLayout();
            SuspendLayout();
            // 
            // ProportionComboBox
            // 
            ProportionComboBox.FormattingEnabled = true;
            ProportionComboBox.Location = new Point(3, 24);
            ProportionComboBox.Margin = new Padding(4);
            ProportionComboBox.Name = "ProportionComboBox";
            ProportionComboBox.Size = new Size(73, 25);
            ProportionComboBox.TabIndex = 3;
            ProportionComboBox.TabStop = false;
            ProportionComboBox.SelectedIndexChanged += ProportionComboBox_SelectedIndexChanged;
            // 
            // panel1
            // 
            panel1.BorderStyle = BorderStyle.FixedSingle;
            panel1.Controls.Add(AddProportion_Button);
            panel1.Controls.Add(checkBL);
            panel1.Controls.Add(textHR);
            panel1.Controls.Add(textWR);
            panel1.Controls.Add(label3);
            panel1.Location = new Point(0, 0);
            panel1.Margin = new Padding(0);
            panel1.Name = "panel1";
            panel1.Size = new Size(140, 53);
            panel1.TabIndex = 0;
            // 
            // AddProportion_Button
            // 
            AddProportion_Button.Location = new Point(73, 1);
            AddProportion_Button.Margin = new Padding(4);
            AddProportion_Button.Name = "AddProportion_Button";
            AddProportion_Button.Size = new Size(63, 24);
            AddProportion_Button.TabIndex = 0;
            AddProportion_Button.TabStop = false;
            AddProportion_Button.Text = "→";
            AddProportion_Button.TextAlign = ContentAlignment.TopCenter;
            AddProportion_Button.UseVisualStyleBackColor = true;
            AddProportion_Button.Click += AddProportion_Button_Click;
            // 
            // checkBL
            // 
            checkBL.Appearance = Appearance.Button;
            checkBL.Font = new Font("Microsoft YaHei UI", 8F);
            checkBL.Location = new Point(2, 1);
            checkBL.Margin = new Padding(0);
            checkBL.Name = "checkBL";
            checkBL.Size = new Size(63, 24);
            checkBL.TabIndex = 0;
            checkBL.TabStop = false;
            checkBL.Text = "锁定比例";
            checkBL.TextAlign = ContentAlignment.MiddleCenter;
            checkBL.UseVisualStyleBackColor = true;
            checkBL.CheckedChanged += checkBL_CheckedChanged;
            // 
            // textHR
            // 
            textHR.Location = new Point(73, 26);
            textHR.Margin = new Padding(4);
            textHR.Name = "textHR";
            textHR.Size = new Size(63, 23);
            textHR.TabIndex = 2;
            textHR.Text = "0";
            textHR.TextAlign = HorizontalAlignment.Center;
            textHR.TextChanged += textHR_TextChanged;
            textHR.KeyPress += IsAllowInput;
            // 
            // textWR
            // 
            textWR.Location = new Point(2, 26);
            textWR.Margin = new Padding(4);
            textWR.Name = "textWR";
            textWR.Size = new Size(63, 23);
            textWR.TabIndex = 1;
            textWR.Text = "0";
            textWR.TextAlign = HorizontalAlignment.Center;
            textWR.TextChanged += textWR_TextChanged;
            textWR.KeyPress += IsAllowInput;
            // 
            // label3
            // 
            label3.AutoSize = true;
            label3.Location = new Point(63, 29);
            label3.Margin = new Padding(4, 0, 4, 0);
            label3.Name = "label3";
            label3.Size = new Size(20, 17);
            label3.TabIndex = 0;
            label3.Text = "：";
            label3.TextAlign = ContentAlignment.MiddleCenter;
            // 
            // label1
            // 
            label1.Location = new Point(-4, 4);
            label1.Margin = new Padding(4, 0, 4, 0);
            label1.Name = "label1";
            label1.Size = new Size(82, 21);
            label1.TabIndex = 0;
            label1.Text = "选择长宽比";
            label1.TextAlign = ContentAlignment.TopCenter;
            // 
            // panel2
            // 
            panel2.BorderStyle = BorderStyle.FixedSingle;
            panel2.Controls.Add(label7);
            panel2.Controls.Add(label4);
            panel2.Controls.Add(textH);
            panel2.Controls.Add(textW);
            panel2.Controls.Add(button_OpenFormZY);
            panel2.Controls.Add(AddResolution_Button);
            panel2.Controls.Add(checkSD);
            panel2.Controls.Add(label6);
            panel2.Controls.Add(ProportionLabel);
            panel2.Location = new Point(0, 55);
            panel2.Margin = new Padding(4);
            panel2.Name = "panel2";
            panel2.Size = new Size(223, 82);
            panel2.TabIndex = 0;
            // 
            // label7
            // 
            label7.AutoSize = true;
            label7.Location = new Point(164, 4);
            label7.Margin = new Padding(4, 0, 4, 0);
            label7.Name = "label7";
            label7.Size = new Size(20, 17);
            label7.TabIndex = 0;
            label7.Text = "高";
            // 
            // label4
            // 
            label4.AutoSize = true;
            label4.Location = new Point(62, 5);
            label4.Margin = new Padding(4, 0, 4, 0);
            label4.Name = "label4";
            label4.Size = new Size(20, 17);
            label4.TabIndex = 0;
            label4.Text = "宽";
            // 
            // textH
            // 
            textH.Location = new Point(133, 24);
            textH.Margin = new Padding(4);
            textH.Name = "textH";
            textH.Size = new Size(85, 23);
            textH.TabIndex = 5;
            textH.Text = "0";
            textH.TextAlign = HorizontalAlignment.Center;
            textH.TextChanged += textH_TextChanged;
            textH.KeyPress += IsAllowInput;
            // 
            // textW
            // 
            textW.Location = new Point(6, 24);
            textW.Margin = new Padding(4);
            textW.Name = "textW";
            textW.Size = new Size(85, 23);
            textW.TabIndex = 4;
            textW.Text = "0";
            textW.TextAlign = HorizontalAlignment.Center;
            textW.TextChanged += textW_TextChanged;
            textW.KeyPress += IsAllowInput;
            // 
            // button_OpenFormZY
            // 
            button_OpenFormZY.Font = new Font("宋体", 8F);
            button_OpenFormZY.Location = new Point(6, 51);
            button_OpenFormZY.Margin = new Padding(4);
            button_OpenFormZY.Name = "button_OpenFormZY";
            button_OpenFormZY.Size = new Size(118, 24);
            button_OpenFormZY.TabIndex = 0;
            button_OpenFormZY.TabStop = false;
            button_OpenFormZY.Text = "像素占用屏幕大小";
            button_OpenFormZY.UseVisualStyleBackColor = true;
            button_OpenFormZY.Click += button_OpenFormZY_Click;
            // 
            // AddResolution_Button
            // 
            AddResolution_Button.Location = new Point(147, 51);
            AddResolution_Button.Margin = new Padding(4);
            AddResolution_Button.Name = "AddResolution_Button";
            AddResolution_Button.Size = new Size(71, 24);
            AddResolution_Button.TabIndex = 0;
            AddResolution_Button.TabStop = false;
            AddResolution_Button.Text = "→";
            AddResolution_Button.TextAlign = ContentAlignment.TopCenter;
            AddResolution_Button.UseVisualStyleBackColor = true;
            AddResolution_Button.Click += AddResolution_Button_Click;
            // 
            // checkSD
            // 
            checkSD.Appearance = Appearance.Button;
            checkSD.Font = new Font("Microsoft YaHei UI", 8F);
            checkSD.Location = new Point(2, 0);
            checkSD.Margin = new Padding(4);
            checkSD.Name = "checkSD";
            checkSD.Size = new Size(59, 23);
            checkSD.TabIndex = 0;
            checkSD.Text = "锁定比例";
            checkSD.TextAlign = ContentAlignment.MiddleCenter;
            checkSD.UseVisualStyleBackColor = true;
            checkSD.CheckedChanged += checkSD_CheckedChanged;
            // 
            // label6
            // 
            label6.Font = new Font("Microsoft YaHei UI", 7F, FontStyle.Bold);
            label6.Location = new Point(96, 24);
            label6.Margin = new Padding(4, 0, 4, 0);
            label6.Name = "label6";
            label6.Size = new Size(31, 23);
            label6.TabIndex = 0;
            label6.Text = "X";
            label6.TextAlign = ContentAlignment.MiddleCenter;
            // 
            // ProportionLabel
            // 
            ProportionLabel.Font = new Font("Microsoft YaHei UI", 7F);
            ProportionLabel.ForeColor = SystemColors.WindowFrame;
            ProportionLabel.Location = new Point(79, 5);
            ProportionLabel.Name = "ProportionLabel";
            ProportionLabel.Size = new Size(86, 16);
            ProportionLabel.TabIndex = 6;
            ProportionLabel.Text = "0:0";
            ProportionLabel.TextAlign = ContentAlignment.MiddleCenter;
            // 
            // panel3
            // 
            panel3.BorderStyle = BorderStyle.FixedSingle;
            panel3.Controls.Add(ScreenCheck);
            panel3.Controls.Add(label8);
            panel3.Controls.Add(FilterResolutionFromProportions_ComboBox);
            panel3.Controls.Add(ElementView);
            panel3.Location = new Point(225, 0);
            panel3.Margin = new Padding(4);
            panel3.Name = "panel3";
            panel3.Size = new Size(230, 137);
            panel3.TabIndex = 0;
            // 
            // ScreenCheck
            // 
            ScreenCheck.Appearance = Appearance.Button;
            ScreenCheck.AutoSize = true;
            ScreenCheck.Location = new Point(172, 0);
            ScreenCheck.Margin = new Padding(4);
            ScreenCheck.Name = "ScreenCheck";
            ScreenCheck.Size = new Size(54, 27);
            ScreenCheck.TabIndex = 0;
            ScreenCheck.TabStop = false;
            ScreenCheck.Text = "仅屏幕";
            ScreenCheck.UseVisualStyleBackColor = true;
            ScreenCheck.CheckedChanged += ScreenCheck_CheckedChanged;
            // 
            // label8
            // 
            label8.AutoSize = true;
            label8.Location = new Point(5, 4);
            label8.Margin = new Padding(4, 0, 4, 0);
            label8.Name = "label8";
            label8.Size = new Size(68, 17);
            label8.TabIndex = 0;
            label8.Text = "选择长宽比";
            // 
            // FilterResolutionFromProportions_ComboBox
            // 
            FilterResolutionFromProportions_ComboBox.FormattingEnabled = true;
            FilterResolutionFromProportions_ComboBox.Location = new Point(82, 1);
            FilterResolutionFromProportions_ComboBox.Margin = new Padding(4);
            FilterResolutionFromProportions_ComboBox.Name = "FilterResolutionFromProportions_ComboBox";
            FilterResolutionFromProportions_ComboBox.Size = new Size(85, 25);
            FilterResolutionFromProportions_ComboBox.TabIndex = 0;
            FilterResolutionFromProportions_ComboBox.TabStop = false;
            FilterResolutionFromProportions_ComboBox.Text = "无";
            FilterResolutionFromProportions_ComboBox.SelectedIndexChanged += FilterResolutionFromProportions_ComboBox_SelectedIndexChanged;
            // 
            // ElementView
            // 
            ElementView.AutoScroll = true;
            ElementView.Location = new Point(0, 30);
            ElementView.Margin = new Padding(4);
            ElementView.Name = "ElementView";
            ElementView.Size = new Size(230, 104);
            ElementView.TabIndex = 0;
            ElementView.TabStop = true;
            // 
            // panel6
            // 
            panel6.BorderStyle = BorderStyle.FixedSingle;
            panel6.Controls.Add(ProportionComboBox);
            panel6.Controls.Add(label1);
            panel6.Location = new Point(142, 0);
            panel6.Margin = new Padding(4);
            panel6.Name = "panel6";
            panel6.Size = new Size(81, 53);
            panel6.TabIndex = 0;
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(455, 138);
            Controls.Add(panel3);
            Controls.Add(panel2);
            Controls.Add(panel6);
            Controls.Add(panel1);
            FormBorderStyle = FormBorderStyle.FixedSingle;
            ImeMode = ImeMode.Off;
            Margin = new Padding(4);
            MaximizeBox = false;
            Name = "Form1";
            ShowIcon = false;
            Text = "分辨率计算器";
            FormClosing += Form1_FormClosing;
            panel1.ResumeLayout(false);
            panel1.PerformLayout();
            panel2.ResumeLayout(false);
            panel2.PerformLayout();
            panel3.ResumeLayout(false);
            panel3.PerformLayout();
            panel6.ResumeLayout(false);
            ResumeLayout(false);
        }

        #endregion

        private ComboBox ProportionComboBox;
        private Panel panel1;
        private TextBox textHR;
        private TextBox textWR;
        private Label label3;
        private Label label1;
        private Panel panel2;
        private Label label6;
        private TextBox textH;
        private Label label7;
        private TextBox textW;
        private Label label4;
        private Panel panel3;
        private Panel panel6;
        private Label label8;
        private FlowLayoutPanel ElementView;
        private ComboBox FilterResolutionFromProportions_ComboBox;
        private CheckBox ScreenCheck;
        private CheckBox checkBL;
        private CheckBox checkSD;
        private Label ProportionLabel;
        private Button AddResolution_Button;
        private Button button_OpenFormZY;
        private Button AddProportion_Button;
        #endregion


        public static Form1? Current { get; set; }
        float ProportionWidth, ProportionHeight;
        void setHtext() { textH.Text = Math.Floor((float.Parse(textW.Text) * ProportionHeight / ProportionWidth)).ToString(); }
        void setWtext() { textW.Text = Math.Floor((float.Parse(textH.Text) * ProportionWidth / ProportionHeight)).ToString(); }
        void setProportionLabel() 
        {
            int w = int.Parse(textW.Text), h = int.Parse(textH.Text);
            if (w == 0 || h == 0)
                return;
            var p = ResolutionData.GetProportion(w, h);
            ProportionLabel.Text = $"{Math.Round((float)w / h, 5, MidpointRounding.ToNegativeInfinity)}->{p.Width}:{p.Height}";
        }
        public void IsAllowInput(object? sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 8) { return; }//退格
            if (e.KeyChar == 46) { return; }
            if (e.KeyChar >= 48 && e.KeyChar <= 57) { return; }

            { e.Handled = true; return; }
        }


        bool wAh = true; //true = w false=h



#pragma warning disable CS8618 // 在退出构造函数时，不可为 null 的字段必须包含非 null 值。请考虑添加 "required" 修饰符或声明为可为 null。
        public Form1()
#pragma warning restore CS8618 // 在退出构造函数时，不可为 null 的字段必须包含非 null 值。请考虑添加 "required" 修饰符或声明为可为 null。
        {
            Current = this;
            InitializeComponent();


            UpdateProportionsUI();
            UpdateResolutionFromProportionsUI();


            ElementViewupDate();
        }

        private void UpdateProportionsUI()
        {
            if (ProportionComboBox.DataSource!=null)
            {
                ProportionComboBox.DataSource = null;
                ProportionComboBox.Update();
            }
            ProportionComboBox.DataSource = AppConfigDataCollection.GlobalConfig.ProportionDatas;
            ProportionComboBox.DisplayMember = "ProportionLabe";
            //ProportionComboBox.ValueMember = "Proportion";
            ProportionComboBox.Update();
        }
        public void UpdateResolutionFromProportionsUI()
        {
            if(FilterResolutionFromProportions_ComboBox.DataSource!=null)
            {
                FilterResolutionFromProportions_ComboBox.DataSource = null;
                FilterResolutionFromProportions_ComboBox.Update();
            }
            FilterResolutionFromProportions_ComboBox.DataSource = AppConfigDataCollection.GlobalConfig.ProportionsinInResolutionDatas;
            FilterResolutionFromProportions_ComboBox.DisplayMember = "ProportionLabe";
            //FilterResolutionFromProportions_ComboBox.ValueMember = "Proportion";
            FilterResolutionFromProportions_ComboBox.Update();
        }

        private void textW_TextChanged(object? sender, EventArgs e)
        {
            if (sender == null) return;
            if (((TextBox)sender).Focused)
            {
                wAh = true;
                if (checkBL.Checked || checkSD.Checked)
                {
                    setHtext();
                }

            }
            setProportionLabel();
        }

        private void textH_TextChanged(object? sender, EventArgs? e)
        {
            if (sender == null) return;
            if (((TextBox)sender).Focused)
            {
                wAh = false;
                if (checkBL.Checked || checkSD.Checked)
                {
                    setWtext();
                }

            }
            setProportionLabel();
        }

        private void checkBL_CheckedChanged(object? sender, EventArgs? e)
        {
            if (checkBL.Checked)
            {
                checkSD.Checked = false;

                ProportionWidth = float.Parse(textWR.Text);
                ProportionHeight = float.Parse(textHR.Text);
                if (wAh)
                    setHtext();
                else
                    setWtext();
            }
        }

        private void checkSD_CheckedChanged(object? sender, EventArgs? e)
        {
            if (checkSD.Checked)
            {
                checkBL.Checked = false;

                ProportionWidth = 1;
                ProportionHeight = (float.Parse(textH.Text) / float.Parse(textW.Text));
            }
        }

        private void textWR_TextChanged(object? sender, EventArgs? e)
        {
            ProportionWidth = float.Parse(textWR.Text);
        }

        private void textHR_TextChanged(object? sender, EventArgs? e)
        {
            ProportionHeight = float.Parse(textHR.Text);
        }

        private void ScreenCheck_CheckedChanged(object? sender, EventArgs? e)
        {
            ElementViewupDate();
        }

        private void FilterResolutionFromProportions_ComboBox_SelectedIndexChanged(object? sender, EventArgs? e)
        {
            ElementViewupDate();
        }

        public class ResolutionContextMenuItem : ToolStripButton 
        {
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
            public ResolutionData ResolutionData { get; init; } 
            public ResolutionContextMenuItem(string text, ResolutionData resolutionData) : base(text) 
            {
                this.ResolutionData = resolutionData;
            }
            protected override void OnClick(EventArgs e)
            {
                base.OnClick(e);
                AppConfigDataCollection.Resolutions.Remove(ResolutionData);
                AppConfigDataCollection.Save();
                Current?.UpdateResolutionFromProportionsUI();
            }

        }
        void ElementViewupDate()
        {
            ElementView.Controls.Clear();

            ResolutionData? Selected = (ResolutionData?)FilterResolutionFromProportions_ComboBox.SelectedItem;
            IEnumerable<ResolutionData> Datas;
            if (Selected == null)
                return;
            else if (Selected == ResolutionData.Zero)
            {
                if (ScreenCheck.Checked)
                    Datas = AppConfigDataCollection.GlobalConfig.ResolutionDatas.Where(v => v.IsScreen);
                else
                    Datas = AppConfigDataCollection.GlobalConfig.ResolutionDatas;
            }
            else if (ScreenCheck.Checked)
                Datas = AppConfigDataCollection.GlobalConfig.ResolutionDatas.Where(v => v.IsScreen && v.ProportionEquals(Selected));
            else
                Datas = AppConfigDataCollection.GlobalConfig.ResolutionDatas.Where(v => v.ProportionEquals(Selected));

            foreach (ResolutionData v in Datas.OrderBy(p => p))
            {
                Label lab = new Label();

                lab.Text = $"{v.Resolution.Width}x{v.Resolution.Height}";
                lab.Click += (a, b) =>
                {
                    SetResolutionValueToUI(v);
                };
                lab.ContextMenuStrip = new ContextMenuStrip();
                lab.ContextMenuStrip.Items.Add(new ResolutionContextMenuItem("删除", v));
                ElementView.Controls.Add(lab);
            }
        }



        private void ProportionComboBox_SelectedIndexChanged(object? sender, EventArgs? e)
        {
            if (ProportionComboBox.SelectedValue == null) return;
            SetProportionValueToUI((ProportionData)ProportionComboBox.SelectedValue);
        }

        void SetProportionValueToUI(ProportionData value)
        {
            ProportionWidth = value.ProportionWidth;
            textWR.Text = ProportionWidth.ToString();

            ProportionHeight = value.ProportionHeight;
            textHR.Text = ProportionHeight.ToString();
        }
        void SetProportionValueToUI(ResolutionData value)
        {
            ProportionWidth = value.ProportionWidth;
            textWR.Text = ProportionWidth.ToString();

            ProportionHeight = value.ProportionHeight;
            textHR.Text = ProportionHeight.ToString();
        }
        void SetResolutionValueToUI(ResolutionData resolutionData)
        {
            SetProportionValueToUI(resolutionData);
            textW.Text = resolutionData.ResolutionWidth.ToString();
            textH.Text = resolutionData.ResolutionHeight.ToString();
        }

        private void AddProportion_Button_Click(object? sender, EventArgs? e)
        {
            ProportionData newData = new(int.Parse(textWR.Text), int.Parse(textHR.Text));
            if (AppConfigDataCollection.StandardProportions.Any(p => p.Equals(newData)))
                return;
            AppConfigDataCollection.StandardProportions.Add(newData);
            AppConfigDataCollection.Save();
            UpdateProportionsUI();
            ProportionComboBox.SelectedIndex = AppConfigDataCollection.StandardProportions.IndexOf(newData);
        }

        private void AddResolution_Button_Click(object? sender, EventArgs? e)
        {
            bool IsScreen;
            DialogResult dr = MessageBoxEx.Show(this,"该保存是否是屏幕分辨率？", "分辨率类型设置", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes) IsScreen = true;
            else IsScreen = false;

            ResolutionData m_data = new ResolutionData(int.Parse(textW.Text), int.Parse(textH.Text), IsScreen);

            if (AppConfigDataCollection.GlobalConfig.ResolutionDatas.Any(p => p.Equals(m_data)))
                return;

            AppConfigDataCollection.GlobalConfig.ResolutionDatas.Add(m_data);

            AppConfigDataCollection.Save();
            UpdateResolutionFromProportionsUI();
        }

        private void Form1_FormClosing(object? sender, FormClosingEventArgs? e)
        {
            this.ProportionComboBox.SelectedIndexChanged -= this.ProportionComboBox_SelectedIndexChanged;
        }

        private void button_OpenFormZY_Click(object? sender, EventArgs? e)
        {
            Form_zy Zy = new Form_zy();
            int w, h;
            try
            {
                w = int.Parse(textW.Text);
                h = int.Parse(textH.Text);
            }
            catch
            {

                MessageBox.Show("当前长宽参数格式错误，无法正确转换类型到int");

                return;
            }

            Zy.setWindowsSize(w, h);
            Zy.ShowDialog(this);
        }

        

        
    }
}


